package com.ganzi.ganziserver.conf.rabbit;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;


/**
 * @Author:lxd
 * @Date:2019/02/16 14:50
 */
@Configuration
public class RabbitMqConfig {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Bean
    public AmqpTemplate AmqpTemplate() {
        Logger log = LoggerFactory.getLogger(RabbitTemplate.class);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.setEncoding("UTF-8");
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback(returnCallBack());
        rabbitTemplate.setConfirmCallback(confirmCallBack());
        return rabbitTemplate;
    }
    @Bean
    public RabbitReturnCallBack returnCallBack() {
        RabbitReturnCallBack returnCallBack = new RabbitReturnCallBack();
        return returnCallBack;
    }
    @Bean
    public RabbitConfirmCallback confirmCallBack() {
        RabbitConfirmCallback confirmCallBack = new RabbitConfirmCallback();
        return confirmCallBack;
    }


//
//
//
//
//    //30个学校队列  每个学校的控制分开处理
//    @Bean(Topic.SCHOOL_1)
//    public Queue schoool1() {
//        Map<String,Object> args=new HashMap<>();
//        args.put("x-dead-letter-exchange", Topic.DEAD_LETTER_EXCHANGE);
//        args.put("x-dead-letter-routing-key", Topic.DEAD_ROUTING_KEY);
//        return QueueBuilder.durable(Topic.SCHOOL_1).withArguments(args).build();
//    }
//    @Bean(Topic.SCHOOL_2)
//    public Queue schoool2() {
//        Map<String,Object> args=new HashMap<>();
//        args.put("x-dead-letter-exchange", Topic.DEAD_LETTER_EXCHANGE);
//        args.put("x-dead-letter-routing-key", Topic.DEAD_ROUTING_KEY);
//        return QueueBuilder.durable(Topic.SCHOOL_2).withArguments(args).build();
//    }
//
//
//    //三个接收队列   每个队列处理10个学校的反馈信息,每个队列绑定多个接收者
//    @Bean(Topic.RECEVIE_ONE)
//    public Queue recevieOne() {
//        return QueueBuilder.durable(Topic.RECEVIE_ONE).build();
//    }
//    @Bean(Topic.RECEVIE_TOW)
//    public Queue recevieTow() {
//        return QueueBuilder.durable(Topic.RECEVIE_TOW).build();
//    }
//    @Bean(Topic.RECEVIE_THREE)
//    public Queue recevieThree() {
//        return QueueBuilder.durable(Topic.RECEVIE_THREE).build();
//    }
//    //接收在线状态队列
//    @Bean(Topic.RECEVIE_STATE)
//    public Queue recevieState() {
//        return QueueBuilder.durable(Topic.RECEVIE_STATE).build();
//    }
//
//    //交换机------------------------
//    @Bean(Topic.EXCHANGE_FOR_CONTROL)
//    public Exchange controlTopicExchange() {
//        return ExchangeBuilder.topicExchange(Topic.EXCHANGE_FOR_CONTROL).durable(true).build();
//    }
//    @Bean(Topic.EXCHANGE_FOR_RECEIVE)
//    public Exchange receiveTopicExchange() {
//        return ExchangeBuilder.topicExchange(Topic.EXCHANGE_FOR_RECEIVE).durable(true).build();
//    }
//
//
//    //绑定控制交换机-----------------------------
//    @Bean
//    public Binding controlTopicBinding1(@Qualifier(Topic.SCHOOL_1) Queue queue,
//                                 @Qualifier(Topic.EXCHANGE_FOR_CONTROL) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with(Topic.SCHOOL_1).noargs();
//    }
//    @Bean
//    public Binding controlTopicBinding2(@Qualifier(Topic.SCHOOL_2) Queue queue,
//                                 @Qualifier(Topic.EXCHANGE_FOR_CONTROL) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with(Topic.SCHOOL_2).noargs();
//    }
//
//
//
//
//    //绑定接收交换机-----------------------------
//    @Bean
//    public Binding receivecBindingOne(@Qualifier(Topic.RECEVIE_ONE) Queue queue,
//                                        @Qualifier(Topic.EXCHANGE_FOR_RECEIVE) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with(Topic.RECEVIE_ONE).noargs();
//    }
//    @Bean
//    public Binding receiveBindingTow(@Qualifier(Topic.RECEVIE_TOW) Queue queue,
//                                        @Qualifier(Topic.EXCHANGE_FOR_RECEIVE) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with(Topic.RECEVIE_TOW).noargs();
//    }
//    @Bean
//    public Binding receiveBindingThree(@Qualifier(Topic.RECEVIE_THREE) Queue queue,
//                                        @Qualifier(Topic.EXCHANGE_FOR_RECEIVE) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with(Topic.RECEVIE_THREE).noargs();
//    }
//    @Bean
//    public Binding receiveBindingState(@Qualifier(Topic.RECEVIE_STATE) Queue queue,
//                                        @Qualifier(Topic.EXCHANGE_FOR_RECEIVE) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with(Topic.RECEVIE_STATE).noargs();
//    }
//
//    //死信队列
//    @Bean(Topic.DEAD_ROUTING_KEY)
//    public Queue dead() {
//        return QueueBuilder.nonDurable(Topic.DEAD_ROUTING_KEY).build();
//    }
//    @Bean(Topic.DEAD_LETTER_EXCHANGE)
//    public Exchange deadExchange() {
//        return ExchangeBuilder.topicExchange(Topic.DEAD_LETTER_EXCHANGE).durable(true).build();
//    }
//    @Bean
//    public Binding deadBinding(@Qualifier(Topic.DEAD_ROUTING_KEY) Queue queue,
//                               @Qualifier(Topic.DEAD_LETTER_EXCHANGE) Exchange exchange) {
//        return BindingBuilder.bind(queue).to(exchange).with(Topic.DEAD_ROUTING_KEY).noargs();
//    }
}
